package com.longcai.util;


import com.alibaba.fastjson.JSONObject;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.NTbkOrder;
import com.taobao.api.internal.util.StringUtils;
import com.taobao.api.request.TbkItemInfoGetRequest;
import com.taobao.api.request.TbkOrderGetRequest;
import com.taobao.api.response.TbkItemInfoGetResponse;
import com.taobao.api.response.TbkOrderGetResponse;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 淘宝客精选公共类
 * </p>
 *
 * @author LWK
 * @since 2019-03-16
 */
public class ChoiceTBKUtil {
    private static final String SERVER_URL_TBK = "https://eco.taobao.com/router/rest";            //服务地址
    private static final String APPKEY_TBK = "25920036";                                //联盟分配给应用的app_key
    private static final String APPSECRET_TBK = "6a3c92d5ba72654ec51a5bcc28c273d8";        //应用app密钥
    private static final String APPKEY_DTK = "056aa8203e";            //大淘客应用app密钥

    /**
     * 大淘客商品展示
     *
     * @param    pageIndex    页码
     */
    public static Map<String, Object> getTBKData(String pageIndex) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //请求的输入流
        BufferedReader in = null;
        //输入流的缓冲
        StringBuffer sb = new StringBuffer();
        try {
            URL url = new URL("http://api.dataoke.com/index.php?r=Port/index&type=total&appkey=" + APPKEY_DTK + "&v=2&page=" + pageIndex);
            in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
            String str = null;
            //一行一行进行读入
            while ((str = in.readLine()) != null) {
                sb.append(str);
            }
            System.out.println(sb.toString());
            resultMap.put("data", JSONObject.parse(sb.toString()));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 大淘客首页商品展示
     */
    public static Map<String, Object> getTBKDataHome() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //请求的输入流
        BufferedReader in = null;
        //输入流的缓冲
        StringBuffer sb = new StringBuffer();
        try {
            URL url = new URL("http://api.dataoke.com/index.php?r=Port/index&type=paoliang&appkey=056aa8203e&v=2");
            in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
            String str = null;
            //一行一行进行读入
            while ((str = in.readLine()) != null) {
                sb.append(str);
            }
            System.out.println(sb.toString());
            resultMap.put("data", JSONObject.parse(sb.toString()));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 大淘客商品详情
     *
     * @param    id    商品id
     * @param    goodsId    淘宝id
     */
    public static Map<String, Object> getTbkDetail(String id, String goodsId, HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //请求的输入流
        BufferedReader in = null;
        //输入流的缓冲
        StringBuffer sb = new StringBuffer();
        try {
            TaobaoClient client = new DefaultTaobaoClient(SERVER_URL_TBK, APPKEY_TBK, APPSECRET_TBK);
            TbkItemInfoGetRequest req = new TbkItemInfoGetRequest();
            req.setNumIids(goodsId);
            req.setPlatform(2L);
            //获取当前ip
            String ipAddr = IpUtils.getIpAddr(request);
            req.setIp(ipAddr);
            TbkItemInfoGetResponse rsp = client.execute(req);
            String body = rsp.getBody();
            resultMap.put("detail1", JSONObject.parse(body));
            URL url = new URL("http://api.dataoke.com/index.php?r=port/index&appkey=056aa8203e&v=2&id=" + id);
            in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
            String str = null;
            //一行一行进行读入
            while ((str = in.readLine()) != null) {
                sb.append(str);
            }
            System.out.println(sb.toString());
            resultMap.put("detail2", JSONObject.parse(sb.toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }


    //淘宝
    public static void main(String[] args) {
        try {
//			getTBKData(1);


            TaobaoClient client = new DefaultTaobaoClient("https://eco.taobao.com/router/rest", "25920036", "6a3c92d5ba72654ec51a5bcc28c273d8");
//			TbkDgItemCouponGetRequest req = new TbkDgItemCouponGetRequest();
//			req.setAdzoneId(106384950456L);
//			req.setPlatform(1L);
//			req.setPageSize(20L);
//			req.setPageNo(1L);
//			TbkDgItemCouponGetResponse rsp = client.execute(req);
//			System.out.println(rsp.getBody());
//			}
            //列表
//			TbkDgItemCouponGetRequest req = new TbkDgItemCouponGetRequest();
//			req.setAdzoneId(106384950456L);
//			req.setPlatform(1L);
//			req.setPageSize(1L);
//			req.setPageNo(1L);
//			TbkDgItemCouponGetResponse rsp = client.execute(req);
//			System.out.println(rsp.getBody());
//			
            //订单
            TbkOrderGetRequest req = new TbkOrderGetRequest();
            req.setFields("tb_trade_parent_id,tb_trade_id,num_iid,item_title,item_num,price,pay_price,seller_nick,seller_shop_title,commission,commission_rate,unid,create_time,earning_time,tk3rd_pub_id,tk3rd_site_id,tk3rd_adzone_id,relation_id,tb_trade_parent_id,tb_trade_id,num_iid,item_title,item_num,price,pay_price,seller_nick,seller_shop_title,commission,commission_rate,unid,create_time,earning_time,tk3rd_pub_id,tk3rd_site_id,tk3rd_adzone_id,special_id,click_time");
            req.setStartTime(StringUtils.parseDateTime("2019-04-23 14:29:00"));
            req.setPageNo(1L);
            req.setSpan(1200L);
            req.setPageSize(20L);
            req.setTkStatus(1L);
            req.setOrderQueryType("create_time");
            req.setOrderScene(1L);
            req.setOrderCountType(1L);
            TbkOrderGetResponse rsp = client.execute(req);
            List<NTbkOrder> results = rsp.getResults();
            if (results != null && results.size() != 0) {
                for (NTbkOrder nTbkOrder : results) {
                    String unid = nTbkOrder.getUnid();
                }
            }
            System.out.println(rsp.getBody());
            //渠道邀请码生成
//			TbkScInvitecodeGetRequest req = new TbkScInvitecodeGetRequest();
//			req.setRelationId(2103383682L);
//			req.setRelationApp("common");
//			req.setCodeType(1L);
//			HttpServletRequest request;
//			TbkScInvitecodeGetResponse rsp = client.execute(req,"1");
//			System.out.println(rsp.getBody());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

